from django import template
register = template.Library()
from sorl.thumbnail.main import DjangoThumbnail
from django.utils.safestring import mark_safe

def thumbnail(value):
    '''
    Filter to generate thumbnail of image
    By default size (200, 200)
    '''
    try:
        t = DjangoThumbnail(relative_source=value, requested_size=(130,130))
        img_url = t.absolute_url
        img_url = img_url.split('/media/')[1]
        output = []
        output.append((u'''<img class="profilImgSize" src="http://127.0.0.1:8000/static_media/%s" />''' % (img_url)))
        return mark_safe(u''.join(output))
    
    except:
        output = []
        output.append(u'''<img class="profilImgSize" src="http://127.0.0.1:8000/static_media/photo/default/NoImage_jpg_200x200_q85.jpg" />''')
        return mark_safe(u''.join(output))
        
register.filter('thumbnail', thumbnail)

